package org.lg.configure;

import com.sun.net.httpserver.HttpServer;
import com.sun.net.httpserver.HttpsServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.net.InetSocketAddress;


@Configuration
@EnableConfigurationProperties(LgServerProperties.class) //打开properties配置，springboot默认读取classPath下的applicaiton.properties 或者application.yml 配置了这个注解默认读取默认配置之外还要额外读取自定义配置
//自定义配置 在 LgServerProperties.class 中
public class LgServerConfiguration {
    private Logger logger = LoggerFactory.getLogger(LgServerConfiguration.class);
    @Bean //也会让spring产生一个对象但是是方法返回值的对象
    @ConditionalOnClass(HttpsServer.class)//当容器中存在对应的class才生成对象，满足条件才生成
    public HttpServer httpServer() throws IOException {
        HttpServer httpServer = HttpServer.create(new InetSocketAddress(LgServerProperties.getPort()),0);
        httpServer.start();
        logger.info("服务器启动:端口为"+LgServerProperties.getPort());
        return httpServer;
    };
}
